package com.iwunu.iot.common.util;

import com.alibaba.fastjson.JSON;

/**
 * @author kinfeng
 * @date 2019-04-25 14:32
 * @desc
 */
public class SmsUtil {
    public static final String charset = "utf-8";
    // 用户平台API账号(非登录账号,示例:N1234567)
    public static String account = "N5765241";
    // 用户平台API密码(非登录密码)
    public static String pswd = "byAvJRjK2w6581";

    /**
     * 普通短信发送
     * @param phone 手机号码，多个号码使用","分割
     * @param msg 短信内容
     * @return 返回值定义参见HTTP协议文档
     * @throws Exception
     */
    public static String sendSms(String phone, String msg) throws Exception{
        //请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取
        String smsSingleRequestServerUrl = "http://smsbj1.253.com/msg/send/json";
        //状态报告
        String report= "true";
        try{
            SmsSendRequest smsSingleRequest = new SmsSendRequest(account, pswd, msg, phone,report);
            String requestJson = JSON.toJSONString(smsSingleRequest);
            System.out.println("before request string is: " + requestJson);
            String response = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
            System.out.println("response after request result is :" + response);
            SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
            return smsSingleResponse.toString();
        }catch(Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public static String sendSmsCode(String phone,Integer random){
        String smsVariableRequestUrl = "http://smsbj1.253.com/msg/variable/json";
        //设置您要发送的内容：其中“【】”中括号为运营商签名符号，多签名内容前置添加提交
        String msg = "【悟牛科技】{$var}，是您的登录验证码，请在{$var}分钟内按页面提示提交验证码，切勿泄露给他人，如非本人操作请忽略本短信。";

        //参数组  "159*******,先生,123456,3;130********,先生,123456,3;";
        String params = phone +","+random+",1;";
        //状态报告
        String report= "true";

        SmsVariableRequest smsVariableRequest=new SmsVariableRequest(account, pswd, msg, params, report);

        String requestJson = JSON.toJSONString(smsVariableRequest);

        System.out.println("before request string is: " + requestJson);

        String response = ChuangLanSmsUtil.sendSmsByPost(smsVariableRequestUrl, requestJson);

        System.out.println("response after request result is : " + response);

        SmsVariableResponse smsVariableResponse = JSON.parseObject(response, SmsVariableResponse.class);

        System.out.println("response  toString is : " + smsVariableResponse);

        return smsVariableResponse.toString();

    }

    public static void main(String[] args) {
    }
}
